﻿using System;
using System.Collections.Generic;
using System.Text;
using ZhCun.Framework.Common.Models.TableModel;

namespace ZhCun.Framework.DataAccess.BuildSQLText
{
    public class BuildSQL_SQLite : BuildSQL
    {
        public override void GenerateTopCountSQLText<T>(int topCount, string sqlWhere, Dictionary<string, object> dbParams, string[] orderFields, bool isDesc, out string sqlText, params string[] selectFields)
        {
            StringBuilder strSql = new StringBuilder();
            if (selectFields == null || selectFields.Length == 0)
            {
                selectFields = GetAllFields<T>();
            }
            string tableName = GetTableName<T>();
            string searchFieldStr = GetSelectFieldStr(selectFields);
            strSql.AppendFormat("Select {0} From {1} ", searchFieldStr, tableName);
            if (CheckSqlWhere(ref sqlWhere))
            {
                strSql.Append(sqlWhere);
            }
            else
            {
                dbParams = null;
            }
            string orderFieldStr;
            if (orderFields != null && orderFields.Length > 0)
            {
                orderFieldStr = GetSelectFieldStr(orderFields);
            }
            else
            {
                orderFieldStr = GetPrimaryKeyName<T>();
            }
            strSql.AppendFormat(" Order By {0} ", orderFieldStr);
            if (isDesc) strSql.AppendLine("desc");
            strSql.AppendFormat("limit {0} offset 0", topCount);
            sqlText = strSql.ToString();
        }

        public override void GeneratePageSQLTextAndParam<T>(int onePageCount, int pageNo, bool isDesc, string[] orderFields, int recordCount, string sqlWhere, Dictionary<string, object> dbParams, out string sqlText, params string[] selectFields)
        {
            if (pageNo == 1)
            {
                //第一页使用top来查询
                GenerateTopCountSQLText<T>(onePageCount, sqlWhere, dbParams, orderFields, isDesc, out sqlText, selectFields);
                return;
            }

            StringBuilder strSql = new StringBuilder();
            if (selectFields == null || selectFields.Length == 0)
            {
                selectFields = GetAllFields<T>();
            }
            string tableName = GetTableName<T>();
            string searchFieldStr = GetSelectFieldStr(selectFields);
            strSql.AppendFormat("Select {0} From {1} ", searchFieldStr, tableName);
            if (CheckSqlWhere(ref sqlWhere))
            {
                strSql.Append(sqlWhere);
            }
            else
            {
                dbParams = null;
            }
            string orderFieldStr;
            if (orderFields != null && orderFields.Length > 0)
            {
                orderFieldStr = GetSelectFieldStr(orderFields);
            }
            else
            {
                orderFieldStr = GetPrimaryKeyName<T>();
            }
            strSql.AppendFormat(" Order By {0} ", orderFieldStr);
            if (isDesc) strSql.AppendFormat("desc");
            //pageCount = (int)(Math.Ceiling(recordCount * 1.0 / onePageCount));
            strSql.AppendFormat(" limit {0} offset {1}", onePageCount, onePageCount * (pageNo - 1));
            sqlText = strSql.ToString();
        }
    }
}